//
//  ProblemViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/29.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "ProblemViewController.h"
#import "Base.h"
#import "ReadyDiSignle.h"
#include "Targets.h"
#import "DetailViewController.h"
#import "OrderMessagesignle.h"
#define BLUE [UIColor colorWithRed:128/256.0 green:128/256.0 blue:128/256.0 alpha:1]
@interface ProblemViewController ()
{
    UIButton *_noItemButton;
    UIButton *_saveitenButton;
   
}
@property(nonatomic,strong)UIView *headView;
@property(nonatomic,strong)UIButton *notSavebutton;
@property(nonatomic,strong)UIButton *saveButton;
@property(nonatomic,strong)UILabel *notSaveLabel;
@property(nonatomic,strong)UILabel *saveLabel;
@property(nonatomic,strong)UIScrollView *mainScroll;
@end

@implementation ProblemViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.view.backgroundColor=COLOR;
       
        }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"notSaveArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"saveArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    self.title=@"问题件";
   
    // Do any additional setup after loading the view.
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"notSaveArray"]) 
        {
        [self buju];
        [self nosaveshuju];

       
        
      }
    if ([keyPath isEqualToString:@"saveArray"]) 
        {
        [self buju];
        

        }

    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"notSaveArray"];
    [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"saveArray"];
    [super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)buju
{
    
    self.headView=[[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 45)];
    self.headView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.headView];
    
    self.notSavebutton=[[UIButton  alloc]init];
    self.notSavebutton.frame=CGRectMake(0, 0, WIDTH/2-1, 40);
    
    self.notSaveLabel=[[UILabel alloc] init];
    self.notSaveLabel.frame=CGRectMake(5, 42, WIDTH/2-11, 2);
    self.notSaveLabel.backgroundColor=[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1];
    [self.headView addSubview:self.notSaveLabel];
   
    UILabel *l=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2-1, 10, 2, 20)];
    l.backgroundColor=COLOR;
    [self.headView addSubview:l];
     
    self.saveButton=[[UIButton alloc] init];
    self.saveButton.frame=CGRectMake(WIDTH/2+1, 0, WIDTH/2-1, 40);
    
    self.saveLabel=[[UILabel alloc] init];
    self.saveLabel.frame=CGRectMake(WIDTH/2+5, 42, WIDTH/2-11, 2);
    self.saveLabel.backgroundColor=[UIColor whiteColor];
    [self.headView addSubview:self.saveLabel];
    
    
    [self.notSavebutton setTitle:[NSString stringWithFormat:@"未解决问题件(%d)",[ReadyDiSignle signle].notSaveArray.count] forState:UIControlStateNormal];
    [self.notSavebutton addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.saveButton setTitle:[NSString stringWithFormat:@"已解决问题件(%d)",[ReadyDiSignle signle].saveArray.count] forState:UIControlStateNormal];
    [self.saveButton addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.notSavebutton.backgroundColor=[UIColor whiteColor];
    [self.notSavebutton setTitleColor:[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1] forState:UIControlStateNormal];
    self.saveButton.backgroundColor=[UIColor whiteColor];
    [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.headView addSubview:self.notSavebutton];
    [self.headView addSubview:self.saveButton];
    
//    self.notSavebutton.layer.borderColor=[[UIColor blueColor] CGColor];
//    self.notSavebutton.layer.borderWidth=1.0;
//    
//    self.saveButton.layer.borderColor=[[UIColor blueColor] CGColor];
//    self.saveButton.layer.borderWidth=1.0;
  
   }
-(void)changeAction:(UIButton *)sender
{
    if (sender==self.notSavebutton)
        {
        
        self.notSavebutton.backgroundColor=[UIColor whiteColor];
        [self.notSavebutton setTitleColor:[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1] forState:UIControlStateNormal];
        self.notSaveLabel.backgroundColor=[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1];
        self.saveButton.backgroundColor=[UIColor whiteColor];
        [self.saveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
         self.saveLabel.backgroundColor=[UIColor whiteColor];

        [self nosaveshuju];
       }
    if (sender==self.saveButton)
        {
        
        self.saveButton.backgroundColor=[UIColor whiteColor];
        [self.saveButton setTitleColor:[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1] forState:UIControlStateNormal];
         self.saveLabel.backgroundColor=[UIColor colorWithRed:128/256.0 green:192/256.0 blue:255/256.0 alpha:1];
        self.notSavebutton.backgroundColor=[UIColor whiteColor];
        [self.notSavebutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.notSaveLabel.backgroundColor=[UIColor whiteColor]; 

        [self saveshuju];
       }
}
-(void)nosaveshuju
{
    self.mainScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headView.frame)+5, WIDTH, HEIGHT-CGRectGetMaxY(self.saveButton.frame)-50)];
    self.mainScroll.backgroundColor=COLOR;
    [self.view addSubview:self.mainScroll];
    for (int i=0; i<[ReadyDiSignle signle].notSaveArray.count; i++)
        {
        Targets *targets=[ReadyDiSignle signle].notSaveArray[i];
        _noItemButton=[[UIButton alloc] initWithFrame:CGRectMake(5, 5+45*i, WIDTH-10, 40)];
        _noItemButton.layer.cornerRadius=4;
        [_noItemButton setTitle:targets.oid forState:UIControlStateNormal];
        [_noItemButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _noItemButton.backgroundColor=[UIColor whiteColor];
        
        [_noItemButton setTag:110+i];
        [_noItemButton addTarget:self action:@selector(notSaveAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainScroll addSubview:_noItemButton];
       
       }
    self.mainScroll.contentSize=CGSizeMake(WIDTH, 45*[ReadyDiSignle signle].notSaveArray.count);
}
-(void)saveshuju
{
    for (int i=0; i<[ReadyDiSignle signle].saveArray.count; i++)
        {
        Targets *targets=[ReadyDiSignle signle].saveArray[i];
        _saveitenButton=[[UIButton alloc] initWithFrame:CGRectMake(5, 5+45*i, WIDTH-10, 40)];
        
        [_saveitenButton setTitle:targets.oid forState:UIControlStateNormal];
        [_saveitenButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_saveitenButton setTag:1110+i];
         [_saveitenButton addTarget:self action:@selector(SaveAction:) forControlEvents:UIControlEventTouchUpInside];
         _saveitenButton.layer.cornerRadius=4;
        _saveitenButton.backgroundColor=[UIColor whiteColor];
        [self.mainScroll addSubview:_saveitenButton];
        }
   self.mainScroll.contentSize=CGSizeMake(WIDTH, 45*[ReadyDiSignle signle].saveArray.count);
}
-(void)notSaveAction:(UIButton *)sender
{
    Targets *targets=[ReadyDiSignle signle].notSaveArray[sender.tag-110];
    [OrderMessagesignle single].oidString=targets._id;
    
    [[OrderMessagesignle single] orderMessagworking];
    
    
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)SaveAction:(UIButton *)sender
{
    Targets *targets=[ReadyDiSignle signle].saveArray[sender.tag-1110];
    [OrderMessagesignle single].oidString=targets._id;
    
    [[OrderMessagesignle single] orderMessagworking];
    
    
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];
}
@end
